import os

class SVG:
	def __init__(self,filename,res):
		""" Create svg file with name, and resolution (tuple) """
		self.f = open(filename,"w")
		self.f.write('<svg width="%d" height="%d" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg">' % res)
		self.resolution = res
		self.fill = "white"
		self.stroke = "black"
		self.strokewidth = 1

	def __del__(self):
		self.f.write('</svg>')
		self.f.close()

	def _line(self,x1,y1,x2,y2):
		self.f.write('<line x1="%d" y1="%d" x2="%d" y2="%d" stroke-width="%d" stroke="%s" />' % (x1,y1,x2,y2,self.strokewidth,self.stroke))
	def set_3d_cartesian_basis(self,o,x,y,z):
		""" Get pairs of 2d coordinates: position of the system, and basis vectors """
		self.o=o
		self.x=x
		self.y=y
		self.z=z

	def line(self,x1,y1,z1,x2,y2,z2):
		self._line(self.o[0]+self.x[0]*x1+self.y[0]*y1+self.z[0]*z1,self.o[1]+self.x[1]*x1+self.y[1]*y1+self.z[1]*z1,
			self.o[0]+self.x[0]*x2+self.y[0]*y2+self.z[0]*z2,self.o[1]+self.x[1]*x2+self.y[1]*y2+self.z[1]*z2)
		

def f(x,y):
	return (x*x+y*y)/10.0

svg = SVG("paraboloid.svg",(800,600))
svg.set_3d_cartesian_basis((400,500),(20,0),(0,-20),(-13,13))

svg.stroke="green"
for i in xrange(-10,11):
	svg.line(-10,0,i,10,0,i)
	svg.line(i,0,-10,i,0,10)

svg.strokewidth = 5
svg.stroke="red"
svg.line(0,0,0,10,0,0)
svg.stroke="green"
svg.line(0,0,0,0,10,0)
svg.stroke="blue"
svg.line(0,0,0,0,0,10)

svg.stroke ="black"
svg.strokewidth=1

for i in xrange(-10,10):
	for j in xrange(-10,10):
		svg.line(i,f(i,j),j,i+1,f(i+1,j),j)
		svg.line(i+1,f(i+1,j+1),j+1,i+1,f(i+1,j),j)
		svg.line(i+1,f(i+1,j+1),j+1,i,f(i,j+1),j+1)
		svg.line(i,f(i,j),j,i,f(i,j+1),j+1)


#del svg
#os.system("opera paraboloid.svg")
